<?php
namespace App\Controllers\Classc;

use App\Libs\Views\Json;
use App\Models\ClassModel\ClassModel;
class CreateAlterClass extends \App\Libs\Base\BaseAction
{
    private $field = [
        'class_name'            => 'class_name',
        'class_icon'            => 'class_icon',  //班级头像兼班级列表图片；
        'class_task'            => 'class_task',  //1:一次不打全扣，2：一次不打口本次；
        'class_task_sel'        => 'class_task_id',//需要任务列表；
        'class_task_period'     => 'class_task_period',  //最大四周
        'class_task_credit'     => 'class_task_credit', //悬赏班总分，||  体验版不需要此字段；
        'class_punch_card'      => 'class_punch_card',      //打卡频率//最大7
        'class_punch_card_type' => 'class_punch_card_type',  //打卡类型：1：图片，2：文字，3：语音
        'class_punch_out'       => 'class_punch_out', //几次不打卡out,不可超过总打卡次数的一半
        'class_type'            => 'class_type',  //2：悬赏，3：体验；，不能创建1：挑战班
        'class_sum_person'      => 'class_sum_person',  //悬赏班才有总人数，体验不要此字段；
        'class_status'          => 'class_status',  //1：显示，2：不显示；
        'class_stime'           => 'class_stime',  //时间，例：2018-11-22 00:00:00
        'class_etime'           => 'class_etime',   //2018-11-22 00:00:00
        'class_recommend'       => 'class_recommend'  //推荐到班级首页，1：不推荐，2：推荐
    ];                     //       class_id:修改传入，添加不传入

    public function execute()
    {

        if($this->request->isPost()){
            if(!$this->request->hasPost('class_type')){
                return Json::view(1,'不能缺少type字段',[]);
            }

            $post = $this->request->getPost();
            $matchField = $this->field;
            //删除多余字段；
            if($post['class_type'] == '2'){ //悬赏班


            }elseif($post['class_type'] == '3'){ //体验版
                unset($matchField['class_sum_person']);
                unset($matchField['class_task_credit']);
            }

            if($this->request->hasPost('class_id')){
                $class_id = $this->request->getPost('class_id');
                $matchClass = ClassModel::findOne($class_id);
                $matchClass->utime = date('Y:m:d H:i:s');
            }else{
                foreach($matchField as $key => $val){
                    if(!isset($post[$val])){
                        return Json::view(1,'缺少字段：'.$val,[]);
                    }
                }
                $matchClass = new ClassModel();
                $matchClass->ctime = date('Y:m:d H:i:s');
                $matchClass->utime = date('Y:m:d H:i:s');
            }

            if(!in_array($post['class_type'],['2','3'])){
                return Json::view(1,'只能创建悬赏班，体验班',[]);
            }
            //检测内容；
            if(isset($post['class_task_period']) && ($post['class_task_period'] <=0 || $post['class_task_period'] >4)){
                return Json::view(1,'班级时长有误，最大4周',[]);
            }
            if(isset($post['class_punch_card']) && ($post['class_punch_card'] >7 || $post['class_punch_card'] <0)){
                return Json::view(1,'打卡频率有误，最多7次/周',[]);
            }
            //不打卡次数检测
            if(isset($post['class_punch_out']) && ($post['class_punch_out'] > 0.5*$post['class_task_period']*$post['class_punch_card'])){
                return Json::view(1,'不打卡次数有误',[]);
            }

            //开始
            foreach($matchField as $key => $val){
//                if(isset($post[$val])){
                    $matchClass->{$key} = $post[$val];
//                }
            }
            //加入id
            if(isset($post['uid'])){
                  $matchClass->uid = $post['uid'];
            }else{
                $matchClass->uid = 0;
            }

            if(!$matchClass->save()){
                return Json::view(1,'保存失败，请重试',[]);
            }else{
                return Json::view(0,'保存成功',[]);
            }
        }else{
            if(!$this->request->hasQuery('class_id')){
                return Json::view(1,'缺少参数',[]);
            }
            $classid = $this->request->getQuery('class_id');
            $matchClass = ClassModel::findOne($classid);
            foreach ($this->field as $key => $val){
                if(isset($matchClass->{$key})){
                    $newData[$val] = $matchClass->{$key};
                }
            }
            $newData['id'] = $matchClass->id;
            $newData['uid'] = $matchClass->uid;

            return Json::view(0,'ok',$newData);
        }

    }
}
